package com.example.usbcamera;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Environment;  
import android.os.Handler;  
import android.text.format.Time;  
import android.view.Menu;  
import android.view.View;  
import android.view.Window;  
import android.view.WindowManager;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ImageView;  
import android.widget.TextView;
import android.widget.Toast; 


public class MainActivity extends Activity {

	private ImageView mImag;  
    private int width = 640;  
    private int height = 480;  
    private String devname = "/dev/video0";  
    private byte[] mdata;  
    private Handler mHandler;  
    private int numbuf = 0;  
    private int index = 0;  
    private int ret = 0; 
    private int Save = 0;
    public Button mcap;  
   // private Bitmap bitmap;
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
          WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		setContentView(R.layout.activity_main);
		
		 
        //mcap = (Button)findViewById(R.id.mcap);
        
        //TextView capture = (TextView) findViewById(R.id.capture);
		//capture.setText("Capture");
		
        numbuf  = 4;  
        mdata = new byte[width * height * numbuf];  
        ret = Fimcgzsd.open(devname.getBytes());  
        if(ret < 0)  
            finish();  
        ret = Fimcgzsd.init(width, height, numbuf);  
        if(ret < 0)  
            finish();  
        ret = Fimcgzsd.streamon();  
        if(ret < 0)  
            finish();  
        mHandler = new Handler();  
        new StartThread().start();  
        //mcap.setOnClickListener(new CaptureListener());  
        
  }  
	public void onClick(View v) {  
    	
    	
    	switch (v.getId()) {
        case R.id.btn_back:
        	Fimcgzsd.disablewaitmode();  
        	MainActivity.this.finish();
            break;
        case R.id.mcap:
        	//Fimcgzsd.saveMyBitmap();
        	Save = 1;
        	//Toast.makeText(MainActivity.this, "Capture Successfully", Toast.LENGTH_LONG).show();
        	break;
        default:
        	MainActivity.this.finish();
        	break;
        }
    }
      
    class StartThread extends Thread {  
  
        @Override  
        public void run() {  
            // TODO Auto-generated method stub  
            super.run();  
            while(true) {  
            	
            	if(Save == 0){
                			index = Fimcgzsd.dqbuf(mdata);  
                			if(index < 0) {  
                				onDestroy();  
                				break;  
                			}  
            			}
            			else{
            				Save = 0; 
            				index = Fimcgzsd.saveMyBitmap();
            			}
            	Fimcgzsd.qbuf(index);
            	
            	/*
            	if(Save == 1){ Save =0;
				index = Fimcgzsd.saveMyBitmap();
                Fimcgzsd.qbuf(index);  }
                */
            }  
        }  
  
    }  
    /*
    public static void saveMyBitmap(Bitmap mBitmap) {  
        Time mtime = new Time();  
        mtime.setToNow();  
        //File fdir = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + "/gzsd/"); 
       // File fdir = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/");
      //  if(!fdir.exists()) {  
       //   //  fdir.mkdir();  
       // }  
        File f = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + mtime.year + mtime.month + mtime.monthDay + mtime.hour + mtime.minute +mtime.second+".png");  
        try {  
                f.createNewFile();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        
        FileOutputStream fOut = null;  
        try {  
                fOut = new FileOutputStream(f);  
                mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);  
                fOut.flush();  
                fOut.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
    }  
      */
    class CaptureListener implements OnClickListener{  
  
        @Override  
        public void onClick(View v) {  
        	
        	
        	//switch (v.getId()) {
           // case R.id.btn_back:
            //	MainActivity.this.finish();
              //  break;
          //  default:
            //	MainActivity.this.finish();
            	//break;
           // }
            // TODO Auto-generated method stub  
           // new CapThread().start();  
            //Fimcgzsd.streamoff();
        	//BitmapFactory.Options opt = new BitmapFactory.Options();
        	//opt.inDither = false;
        	//opt.inPreferredConfig = Bitmap.Config.RGB_565;
        	//opt.inJustDecodeBounds = true;
           
           // byte[] jdata = mdata.toByteArray();
            //if (mdata.length != 0) {
            	//String d = new String(mdata);
            	//Log.i("************",String.valueOf(mdata.length));
            	//bitmap = Bitmap.createScaledBitmap(mdata, width, height,Bitmap.Config.ARGB_8888);
            	//bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            	//bitmap = BitmapFactory.decodeByteArray(mdata, 0, mdata.length);
            	//bitmap = BitmapFactory.decodeStream(mdata, null, opt);  
            	//Log.i("************",String.valueOf(bitmap));
           // } else{
            	//Log.i("===========",String.valueOf(bitmap));
          //  }
            
            //Log.i("===========",String.valueOf(bitmap));
          //  saveMyBitmap(bitmap);  
           // Fimcgzsd.streamon();  
          //  Toast.makeText(MainActivity.this, "Capture Successfully", Toast.LENGTH_LONG).show();  
        	//Log.d("CAM", "wangwenwu test");
        }  
          
    }  
      
    class CapThread extends Thread {  
  
        @Override  
        public void run() {  
            // TODO Auto-generated method stub  
           // super.run();  
           // saveMyBitmap(bitmap);  
            //Toast.makeText(MainActivity.this, "Capture Successfully", Toast.LENGTH_LONG).show();  
        }  
          
    }  
      
    @Override  
    protected void onStop() {  
        // TODO Auto-generated method stub  
        super.onStop();  
        Fimcgzsd.release();  
        finish();  
    }  
  
    @Override  
    protected void onPause() {  
        // TODO Auto-generated method stub  
        super.onPause();  
        Fimcgzsd.release();  
        finish();  
    }  
  
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();  
        Fimcgzsd.release();  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        finish();  
        return true;  
    }  
  
}  